package com.xj.zhihudemo.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.pnikosis.materialishprogress.ProgressWheel;
import com.xj.zhihudemo.R;
import com.xj.zhihudemo.mode.ThemesInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 1016/10/14.
 */

public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {
    private final static int TYPE_HEADER = 0;
    private final static int TYPE_NORMAL = 1;
    private final static int TYPE_LOAD_MORE = 2;
    private final String TAG = "TestAdapter";
    private final LayoutInflater mInflater;

    private ArrayList<ThemesInfo.StoriesEntity> mDatas = new ArrayList<>();
    private ArrayList<Integer> idList = new ArrayList<>();

    //头部视图
    private Context mContext;
    private View headerView;
    private boolean bIsLoading = false;

    public TestAdapter(Context context) {
        Log.d(TAG, "TestAdapter()" + "_______" + mDatas.size());
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }

    public void addHeader(View headerView) {
        Log.d(TAG, "addHeader()" + "_______" + mDatas.size());
        this.headerView = headerView;
        notifyDataSetChanged();
    }

    public void addDatas(List<ThemesInfo.StoriesEntity> list) {
        Log.d(TAG, "addDatas()" + "_______" + mDatas.size());
        mDatas.addAll(list);
        if (mDatas != null) {
            idList.clear();
            for (ThemesInfo.StoriesEntity storiesEntity : mDatas) {
                idList.add(storiesEntity.getId());
            }
        }
        Log.d("adapter", mDatas.size() + "");
        notifyDataSetChanged();
    }

    public void setLoading(boolean loading) {
        Log.d(TAG, "setLoading" + "_______" + mDatas.size());
        this.bIsLoading = loading;
    }

    public boolean canLoadMore() {
        Log.d(TAG, "canLoadMore" + "_______" + mDatas.size());
        return mDatas.size() >= 10 && !bIsLoading;
    }

    @Override
    public int getItemViewType(int position) {
        Log.d(TAG, "getItemViewType()" + "_______" + mDatas.size());
        if (mDatas.size() >= 10 && position == getItemCount() - 1) {
            return TYPE_LOAD_MORE;
        } else {
            if (headerView == null) return TYPE_NORMAL;
            if (position == 0) return TYPE_HEADER;
            return TYPE_NORMAL;
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d(TAG, "onCreateViewHolder" + "_______" + mDatas.size());
        if (viewType == TYPE_HEADER && headerView != null) {
            return new HeaderViewHolder(headerView);
        } else if (viewType == TYPE_LOAD_MORE) {
            return new LoadMoreViewHolder(mInflater.inflate(R.layout.item_load_more, parent, false));
        } else {
            return new NormalViewHolder(mInflater.inflate(R.layout.item_news_image, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Log.d(TAG, "onBindViewHolder" + "_______" + mDatas.size());
        holder.update(position);
    }


    @Override
    public int getItemCount() {
        return mDatas.size() >= 10 ? mDatas.size() + 1 : mDatas.size();
    }

    public abstract class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        protected void update(int position) {
        }
    }

    class HeaderViewHolder extends ViewHolder {

        public HeaderViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void update(int position) {
            super.update(position);
        }
    }

    class LoadMoreViewHolder extends ViewHolder {
        @BindView(R.id.icon_loading)
        ProgressWheel mIconLoading;
        @BindView(R.id.icon_finish)
        TextView mIconFinish;

        public LoadMoreViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void update(int position) {
            super.update(position);
            mIconLoading.setVisibility(bIsLoading ? View.VISIBLE : View.GONE);
            if (bIsLoading) {
                mIconFinish.setText("正在加载……");
                mIconFinish.setVisibility(View.VISIBLE);
            } else {
                mIconFinish.setVisibility(View.GONE);
                mIconFinish.setText("加载更多");
            }

//            mIconFinish.setVisibility(bIsLoading ? View.VISIBLE : View.VISIBLE);
        }
    }

    class NormalViewHolder extends ViewHolder {
        @BindView(R.id.iv_news_image)
        SimpleDraweeView mIvNewsImage;
        @BindView(R.id.tv_news_text)
        TextView mTvNewsText;

        private ThemesInfo.StoriesEntity data;

        public NormalViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void update(int position) {
            super.update(position);
            data = mDatas.get(position);
            if (data.getImages() != null && data.getImages().size() > 0) {
                mIvNewsImage.setImageURI(
                        mDatas.get(position).getImages().get(0));
                mIvNewsImage.setVisibility(View.VISIBLE);
            } else {
                mIvNewsImage.setVisibility(View.GONE);
            }
            mTvNewsText.setText(mDatas.get(position).getTitle());
        }
    }
}
